package com.zsyh.jiujiu.home;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.zyh.toolslib.base.BaseActivity;
import com.example.zyh.toolslib.utils.SPUtils;
import com.example.zyh.toolslib.widget.TitleBar;
import com.google.gson.Gson;
import com.lwb.retrofit.RetrofitTask;
import com.zsyh.jiujiu.Api;
import com.zsyh.jiujiu.R;
import com.zsyh.jiujiu.adepter.OnItemClickLitener;
import com.zsyh.jiujiu.adepter.SingleAdapter;
import com.zsyh.jiujiu.bean.MessageEvn;
import com.zsyh.jiujiu.bean.MyGoodBean;
import com.zsyh.jiujiu.net.MyObserver;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * @ProjectName: livvehelp
 * @Package: com.zsyh.lsb.ui
 * @ClassName:
 * @Description: java类作用描述
 * @Author: zsyh
 * @CreateDate: 2020/9/7 11:59
 * @UpdateUser: liangwanbo
 * @UpdateDate: 2020/9/7 11:59
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class MyGoodActivity extends BaseActivity {

    @BindView(R.id.title_bar)
    TitleBar titleBar;
    @BindView(R.id.rvlist)
    RecyclerView rvlist;
    @BindView(R.id.tijiao)
    TextView tijiao;
    private String data;


    @Override
    protected int initLayout() {
        return R.layout.activity_mygood;
    }

    @Override
    protected void initView() {
        titleBar.setLeftLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    @Override
    protected void initData() {

        MyGood((String) SPUtils.get(thisActivity, "userid", ""),"1");
    }

    @Override
    protected void initEvent() {
    }



    @OnClick(R.id.tijiao)
    public void onViewClicked() {
        EventBus.getDefault().post(new MessageEvn(data, "add"));
        thisActivity.finish();
    }

    /**
     * 我的商品
     */
    public void MyGood(String userid,String stattus){
        RetrofitTask.getInstance()
                .createApi(Api.class)
                .getMyGoods(userid,stattus)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new MyObserver(this,false) {
                    @Override
                    protected void onError(String errorMsg) {

                    }

                    @Override
                    protected void onSuccess(String JsonStr) {
                        Log.e("wode",JsonStr);
                        Gson gson=new Gson();
                        MyGoodBean goodBean=gson.fromJson(JsonStr,MyGoodBean.class);
                        rvlist.setHasFixedSize(true);
                        rvlist.setLayoutManager(new LinearLayoutManager(thisActivity));
                        final SingleAdapter adapter = new SingleAdapter(goodBean.getData(),thisActivity);
                        rvlist.setAdapter(adapter);

                        adapter.setOnItemClickLitener(new OnItemClickLitener() {
                            @Override
                            public void onItemClick(View view, int position) {
                                adapter.setSelection(position);
                                data=goodBean.getData().get(position).toString();
                            }

                            @Override
                            public void onItemLongClick(View view, int position) {

                            }
                        });

                    }
                });

    }
}
